<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-21 10:06
 *
 * Version: 2.0
 */

namespace App\Modules\Cashier\Transformers;


use App\Lib\Utilities;
use App\Modules\Employee\Repositories\EmployeeRepository;
use App\Modules\MemberCard\Repositores\MemberCardRepository;
use League\Fractal\TransformerAbstract;

class GetStoreBillTransformer extends TransformerAbstract
{
	public static function transform($list, $isPad = false)
	{

		$items = array_map(function ($v) use ($isPad) {
			$item_list = [];
			$payTime = '';
			$payment = 1;
			$type = 0;
			if (!empty($v->order_id)) {
				if ($v->category == config('constant.bill.category.order')) {
					$v->order->items;
					$order = $v->order;

					$item_list = isset($v->order->items) ? $v->order->items->toArray() : [];
					$item_list = array_map(function ($v) use ($isPad) {
						return [
							'id'               => $v['service_id'],
							"name"             => $v['service_name'],
							"count"            => $v['count'],
							"price"            => $v['price'],
							"is_appoint"       => isset($v['is_appoint']) ? $v['is_appoint'] : 0,
							"sex"              => isset($v['sex']) ? $v['sex'] : 0,
							"main_recommend"   => EmployeeRepository::getBarberUserInfoById($v['barber_id'], $isPad),
							"assist_recommend" => EmployeeRepository::getBarberUserInfoById($v['assistant_id'], $isPad),
						];
					}, $item_list);

					$payment = $order->payment;
					$payTime = $order->finish_time;
					$type = $order->payment == config('constant.payment.member_card') ? config('constant.bill.type.service_card') : config('constant.bill.type.individual');
					if($payment == config('constant.payment.member_card')){
						$memberCardInfo = MemberCardRepository::getMemberCardName($order->member_card_id);
					}

				}
				if ($v->category == config('constant.bill.category.card')) {

					$memberCardOrder = $v->memberCardOrder;
					$memberCardInfo = $v->memberCardOrder->memberCard;
					array_push($item_list, [
						'id'               => $memberCardInfo->id,
						"name"             => $memberCardInfo->cardType->name,
						"count"            => 1,
						"price"            => $v['order']['price'],
						"is_appoint"       => 0,
						"sex"              => 0,
						"main_recommend"   => EmployeeRepository::getBarberUserInfoById($v['main_recommend'], $isPad),
						"assist_recommend" => EmployeeRepository::getBarberUserInfoById($v['assist_recommend'], $isPad),
					]);

					$payment = $memberCardOrder->payment;
					$payTime = $memberCardOrder->created_at->format('Y-m-d H:i:s');
					$type = $memberCardOrder->category == 1 ? config('constant.bill.type.card_handle') : config('constant.bill.type.recharge');

				}
				if ($v->category == config('constant.bill.category.goods')) {
					$v->goodsOrder->items;
					$goodsOrder = $v->goodsOrder;
					$item_list_obj = isset($v->goodsOrder->items) ? $v->goodsOrder->items : [];

					foreach ($item_list_obj as $sv) {
						$item_list[] = [
							'id'               => $sv->goods_id,
							"name"             => $sv->goods_name,
							'image'            => isset($sv->goods->picture->image) ? $sv->goods->picture->image : '',
							"count"            => $sv->count,
							"price"            => $sv->real_price,
							"is_appoint"       => 0,
							"sex"              => 0,
							"main_recommend"   => EmployeeRepository::getBarberUserInfoById($v->main_recommend, $isPad),
							"assist_recommend" => EmployeeRepository::getBarberUserInfoById($v->assist_recommend, $isPad),
						];
					}
					$payment = $goodsOrder->payment;
					$payTime = $goodsOrder->created_at->format('Y-m-d H:i:s');
					$type = config('constant.bill.type.goods');
				}
			}

			$returnData =  [
				'id'          => $v->id,
				"barber_id"   => $v->user->id,
				'portrait'    => $v->user->portrait,
				"barber_name" => $v->user->nick,
				"category"    => $v->category,
				"amount"      => $v->amount,     // 应付金额
				"receipts"    => isset($v->receipts) ? $v->receipts : '0', // 实收金额
				"order_id"    => $v->order_id,
				"status"      => $v->status,     // 订单状态
				"order_no"    => $v->category == 1 ? $v->order->order_no : '',
				"items"       => $item_list,
				"created_at"  => $v->created_at->format('Y-m-d H:i:s'),
				"pay_time"    => $payTime,
				"payment"     => $payment,
				'type'        => $type,

			];
			if($isPad){
				$returnData['member_card'] = [
//					''


				];
//				$returnData['member_card_type_name'] = isset($memberCardInfo)  ? $memberCardInfo->cardType->name : null;
//				$returnData['member_name'] = isset($memberCardInfo)  ? $memberCardInfo->name : null;
//				$returnData['member_balance'] = isset($memberCardInfo)  ? $memberCardInfo->balance : null;
//				$returnData['member_portrait'] = isset($memberCardInfo)  ? $memberCardInfo->member->face : null;
			}else{
				$returnData['member_card'] = isset($memberCardInfo) ? $memberCardInfo : null;
			}
//			if( ($v->category == 2)  ||($payment == config('constant.payment.member_card'))){
//				$returnData['member_card'] = isset($memberCardInfo) ? $memberCardInfo : null;
//			}

			return $returnData;
		}, $list->items());

		$result = Utilities::dealPage($list);
		$result['items'] = $items;
		return $result;
	}
}